package loengud.w04;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
 * Read data from web.
 * @author Ago
 *
 */
public class ReadFromWeb {

	/**
	 * main.
	 * @param args Arguments
	 */
	public static void main(String[] args) {
		printWebConents("http://neti.ee");

	}

	/**
	 * Opens a connection to specified link, 
	 * reads the contents of the web page and
	 * prints the whole response.
	 * 
	 * @param link Web page address to open and read
	 */
	public static void printWebConents(String link) {
		URL url;
		try {
			url = new URL(link);
			URLConnection connection = url.openConnection();
			BufferedReader reader = new BufferedReader(
					new InputStreamReader(
					connection.getInputStream()));
			String line;
			while ((line = reader.readLine()) != null) {
				System.out.println(line);
			}
		} catch (MalformedURLException e) {
			System.out.println("Malformed URL exception: "
					+ e.getMessage());
			//e.printStackTrace();
		} catch (IOException e) {
			System.out.println("Error reading from " 
					+ link + ": " + e.getMessage());
			//e.printStackTrace();
		}
	}
}
